<template>
  <div class="p-6 bg-base-200/50 rounded-2xl">
    <CommentItem
      :comment="comment"
      :replies="replies"
      :show-replies="true"
      @toggle-like="handleToggleLike"
      @create-reply="handleCreateReply"
      @report-comment="handleReportComment"
      @delete-comment="handleDeleteComment"
      @reply-click="handleReplyClick"
    >
      <!-- 自定义回复头像插槽 -->
      <template #reply-avatar="{ author }">
        <div class="relative group">
          <div
            class="w-10 h-10 rounded-full overflow-hidden border-2 border-secondary/50 shadow-md cursor-pointer transform hover:scale-110 transition-all duration-300"
          >
            <img :src="author.avatar" :alt="author.name" class="w-full h-full object-cover" />
          </div>
          <!-- 在线状态指示器 -->
          <div
            class="absolute -bottom-1 -right-1 w-3 h-3 bg-success rounded-full border border-base-100 shadow-sm"
          ></div>
          <!-- 悬停提示 -->
          <div
            class="absolute -top-8 left-1/2 transform -translate-x-1/2 bg-base-content text-base-100 text-xs px-2 py-1 rounded opacity-0 group-hover:opacity-100 transition-opacity duration-200 whitespace-nowrap"
          >
            {{ author.name }}
          </div>
        </div>
      </template>
    </CommentItem>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { CommentItem } from '@/components/common/comment'
import type { Comment } from '@/apis/methods/comment.api'

// 评论数据
const comment = ref<Comment>({
  id: '1',
  content:
    '这个示例展示了自定义回复头像插槽的用法。回复头像有边框、阴影、悬停缩放效果，还有在线状态指示器和悬停提示。',
  author: {
    id: '1',
    name: '头像专家',
    avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=avatar',
  },
  createdAt: '2024-01-15T10:30:00Z',
  updatedAt: '2024-01-15T10:30:00Z',
  likes: 15,
  isLiked: false,
  replyCount: 2,
  parentId: undefined,
  articleId: '1',
  mentionedUsers: [],
  status: 'approved',
})

// 回复数据
const replies = ref<Comment[]>([
  {
    id: 'r1',
    content: '回复头像设计得很棒！',
    author: {
      id: '2',
      name: '设计师',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=designer',
    },
    createdAt: '2024-01-15T11:00:00Z',
    updatedAt: '2024-01-15T11:00:00Z',
    likes: 3,
    isLiked: false,
    replyCount: 0,
    parentId: '1',
    articleId: '1',
    mentionedUsers: [],
    status: 'approved',
  },
  {
    id: 'r2',
    content: '确实，效果很好！',
    author: {
      id: '3',
      name: '开发者',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=developer',
    },
    createdAt: '2024-01-15T11:30:00Z',
    updatedAt: '2024-01-15T11:30:00Z',
    likes: 2,
    isLiked: false,
    replyCount: 0,
    parentId: '1',
    articleId: '1',
    mentionedUsers: [],
    status: 'approved',
  },
])

// 事件处理函数
const handleToggleLike = (commentId: string) => {
  const targetComment =
    comment.value.id === commentId ? comment.value : replies.value.find((c) => c.id === commentId)
  if (targetComment) {
    targetComment.isLiked = !targetComment.isLiked
    targetComment.likes += targetComment.isLiked ? 1 : -1
  }
}

const handleCreateReply = (parentId: string, content: string) => {
  console.log('创建回复:', { parentId, content })
}

const handleReportComment = (commentId: string) => {
  console.log('举报评论:', commentId)
}

const handleDeleteComment = (commentId: string) => {
  console.log('删除评论:', commentId)
}

// 这些函数在插槽示例中不需要实际使用，但保留用于演示
// const handleAvatarClick = (author: Comment['author']) => {
//   console.log('点击头像:', author)
// }

// const handleAuthorClick = (author: Comment['author']) => {
//   console.log('点击作者:', author)
// }

// const handleContentClick = (comment: Comment) => {
//   console.log('点击内容:', comment)
// }

const handleReplyClick = (reply: Comment) => {
  console.log('点击回复:', reply)
}
</script>

<style scoped>
/* 自定义回复头像样式 */
</style>
